package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.*;
import com.ywgt.system.service.*;
import com.ywgt.system.vo.InStockVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "入库模块",tags ="入库模块" )
@RequestMapping("/business/inStock")
public class InStockController {

    @Autowired
    private InStockService inStockService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private InStockInfoService inStockInfoService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductStockService productStockService;
    @ApiOperation(value = "获取库存列表",notes = "根据条件查询库存列表")
    @GetMapping("/findInStockList")
    public Result findInStockList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize,
                                  InStockVO inStockVO){
        Page<InStockVO> stockVOPage = inStockService.findInStockList(pageNum, pageSize, inStockVO);
        List<InStockVO> inStockVOS = stockVOPage.getResult();
        long total = stockVOPage.getTotal();
        return Result.ok().data("rows",inStockVOS).data("total",total);
    }

    @ApiOperation(value = "查询订单详情",notes ="根据id查询订单详情" )
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id") Long id,@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                         @RequestParam(required = false,value = "pageSize",defaultValue = "3")Integer pageSize){
        InStock inStock = inStockService.detail(id);
        Supplier supplier = supplierService.getById(inStock.getSupplierId());
        List<InStockInfo> stockList = inStock.getStockList();
        for (InStockInfo inStockInfo : stockList) {
            inStockInfo.getProduct().setCount(inStockInfo.getProductNumber());
        }
        List<Product> products = stockList.stream().map(InStockInfo::getProduct).collect(Collectors.toList());
        products= products.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        return  Result.ok().data("rows",products).data("supplier",supplier).data("status",inStock.getStatus()).data("total",stockList.size());
    }
    @ApiOperation(value = "删除订单",notes ="根据id删除订单" )
    @PutMapping("/remove/{id}")
    public Result remove(@PathVariable("id")Long id){

        LambdaUpdateWrapper<InStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(InStock::getId,id);
        updateWrapper.set(InStock::getStatus,1);
        if (inStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "入库审核",notes = "入库单进行审核")
    @PutMapping("/publish/{id}")
    public Result publish(@PathVariable("id")Long id){
        LambdaUpdateWrapper<InStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(InStock::getId,id);
        updateWrapper.set(InStock::getStatus,0);
        if (inStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "入库回滚",notes = "入库单进行回滚操作")
    @PutMapping("/back/{id}")
    public Result back(@PathVariable("id")Long id){
        LambdaUpdateWrapper<InStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(InStock::getId,id);
        updateWrapper.set(InStock::getStatus,0);
        if (inStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "删除入库单",notes = "入库单进行删除")
    @PutMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        if (inStockService.delete(id)){
                return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "添加入库单",notes = "添加入库单")
    @PostMapping("/addIntoStock")
    public Result add(@RequestBody InStockVO inStockVO){
        AuthUser authUser =(AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(inStockVO);
        if (!StringUtils.isEmpty(inStockVO.getSupplierId())){
            boolean flag=inStockService.supplierInsert(inStockVO,authUser,inStockVO.getSupplierId());
        }else {
            inStockService.newInsert(inStockVO,authUser);
        }
        return Result.ok();
    }
}